/**
 * 路由结构和后端菜单数据以及左侧菜单结构保持一致
*/

import routerMap from "./routerMap";

//1,2为菜单和子菜单，3为按钮不显示
function isRouter(menu){
  return menu.type !== 3 && menu.visibleFlag === 1;
}

function buildRouter(menu){
  let children = menu.children ? buildRouters(menu.children) : null;
  if(children && children.length == 0){
    children = null;
  }
  let component = routerMap.get(menu.path);
  if(menu.amisSchema){
    component = routerMap.get('AMIS');
  }
  return {
    path: menu.path || '',
    name: menu.id,
    key: menu.id,
    meta: { 
      title: menu.menuName,
      permission: menu.permission,
      code: menu.menuCode, 
      icon: menu.icon,
      amisSchema: menu.amisSchema,
      amisSchemaApi: menu.amisSchemaApi,
      requiresAuth: true,
      isExternal: menu.path && menu.path.startsWith('http://')
    },
    component: component,
    children: children
  }
}

export function buildRouters(menus){
  if(menus == null || menus.length == 0){
    return null;
  }
  return menus.filter(m => isRouter(m)).map(menu => buildRouter(menu));
}